@nera-static/plugin-images 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +123 -40
- package/bin/publish-template.js +2 -2
- package/config/images.yaml +49 -0
- package/index.js +8 -197
- package/package.json +19 -16
- package/src/cache.js +13 -12
- package/src/config.js +70 -96
- package/src/frontmatter.js +5 -7
- package/src/generate.js +5 -5
- package/src/log.js +7 -0
- package/src/params.js +5 -5
- package/src/process-pages.js +206 -0
- package/src/queue.js +1 -1
- package/src/resolve-source.js +6 -23
- package/src/rewrite-html.js +18 -18
- package/src/transform.js +24 -24
- package/src/guard.js +0 -73
- package/src/warn.js +0 -6
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,26 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.1.0] - 2026-12-01
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- The package ships `config/images.yaml` with every key and its default (`concurrency` commented out, since its default depends on the machine), as documentation to copy from.
|
|
13
|
+
|
|
14
|
+
### Changed
|
|
15
|
+
|
|
16
|
+
- Requires Node.js 22.12.0 or later (was 20.18.1), the real floor of `@nera-static/core` and of the toolchain.
|
|
17
|
+
- The generic helpers come from `@nera-static/plugin-utils` 1.10.0 (config reader, logger, core guard, `isPlainObject`, URL helpers) instead of local copies, so config warnings read like every other Nera plugin's.
|
|
18
|
+
- The core guard is the shared one: a core whose version cannot be determined no longer prints a warning and the build goes on, a prerelease of 4.13.0 (`4.13.0-beta`) now counts as older than 4.13.0 and is refused, and the error reads `plugin-images: @nera-static/core <version> is too old; it needs >= 4.13.0 (the getAssets hook). Update with …`.
|
|
19
|
+
- The build summary goes through the shared logger: `ℹ️ plugin-images: 2 image(s), 6 variant(s) encoded` instead of `🖼️ plugin-images: …`.
|
|
20
|
+
- README follows the outline of every Nera plugin, with new sections Usage, Template Publishing, Styling (the `<picture>`/`<figure>` markup), Generated Output and Warnings.
|
|
21
|
+
|
|
22
|
+
### Fixed
|
|
23
|
+
|
|
24
|
+
- A key named like an `Object.prototype` member (`constructor:`, `toString:`) in `config/images.yaml` is reported as unknown and ignored; before, it passed without a warning and landed in the config.
|
|
25
|
+
- A page whose own frontmatter sets `images:` to a date (`images: 2026-10-01`, which YAML reads as a `Date`) is no longer treated as a mapping that `meta.images` merges into: like any other non-mapping `images:`, it is left alone with a warning.
|
|
26
|
+
- An image cache `manifest.json` whose `entries` is a list is treated as broken and the cache starts empty, instead of being read as valid.
|
|
27
|
+
|
|
8
28
|
## [1.0.3] - 2026-11-18
|
|
9
29
|
|
|
10
30
|
### Changed
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ A plugin for the [Nera](https://github.com/seebaermichi/nera) static site genera
|
|
|
7
7
|
|
|
8
8
|
📖 **Documentation:** [nera.js.org](https://nera.js.org)
|
|
9
9
|
|
|
10
|
-
> Requires `@nera-static/core` **4.13.0 or later** (see [Compatibility](
|
|
10
|
+
> Requires `@nera-static/core` **4.13.0 or later** (see [Compatibility](#-compatibility)).
|
|
11
11
|
|
|
12
12
|
## ✨ Features
|
|
13
13
|
|
|
@@ -35,38 +35,20 @@ Add the cache folder to your `.gitignore`:
|
|
|
35
35
|
.nera-cache
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
##
|
|
38
|
+
## 🧩 Usage
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-

|
|
42
|
-
```
|
|
40
|
+
Put an image into your assets folder, for example `assets/images/pier.jpg` (`theme/assets/images/pier.jpg` on a site with a `theme/` folder), and use it in a page like any image:
|
|
43
41
|
|
|
44
|
-
|
|
42
|
+
```markdown
|
|
43
|
+
---
|
|
44
|
+
layout: pages/default.pug
|
|
45
|
+
title: Usedom
|
|
46
|
+
---
|
|
45
47
|
|
|
46
|
-
|
|
47
|
-
<picture>
|
|
48
|
-
<source type="image/avif" srcset="/_img/zerpensafari-iphone-fca66340-480.avif 480w, /_img/zerpensafari-iphone-fca66340-800.avif 800w" sizes="100vw">
|
|
49
|
-
<source type="image/webp" srcset="/_img/zerpensafari-iphone-881dc739-480.webp 480w, /_img/zerpensafari-iphone-881dc739-800.webp 800w" sizes="100vw">
|
|
50
|
-
<img src="/_img/zerpensafari-iphone-06076855-800.jpg"
|
|
51
|
-
srcset="/_img/zerpensafari-iphone-06076855-480.jpg 480w, /_img/zerpensafari-iphone-06076855-800.jpg 800w"
|
|
52
|
-
sizes="100vw" width="800" height="1739"
|
|
53
|
-
alt="Startseite der ZerpenSafari"
|
|
54
|
-
loading="eager" fetchpriority="high">
|
|
55
|
-
</picture>
|
|
48
|
+

|
|
56
49
|
```
|
|
57
50
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
On the field-test site (a German/English blog), the 32 content images went from 4.2 MB of originals to 0.45 MB of AVIF a phone downloads, and the Lighthouse mobile LCP of a blog post dropped from 3.5–3.8 s to 2.6 s; with its teaser image rendered through the mixin as well (see below), to 1.8–1.9 s.
|
|
61
|
-
|
|
62
|
-
### What is left untouched
|
|
63
|
-
|
|
64
|
-
- SVG and GIF (animation), external `http(s)://` and `//` URLs, `data:` URIs
|
|
65
|
-
- An `<img>` already inside a `<picture>`, or with its own `srcset`
|
|
66
|
-
- An image with `?skip`, or a raw HTML `<img data-nera-skip>` (the attribute is removed from the output)
|
|
67
|
-
- A file that cannot be found: the build warns with the page and the URL and leaves the `<img>` as it is
|
|
68
|
-
|
|
69
|
-
Raw HTML `<img>` in Markdown is handled like a Markdown image. Images in your **Pug templates** are not processed; use frontmatter images and the mixin for those (see below).
|
|
51
|
+
`nera build` writes the page with a `<picture>` in place of the bare `<img>` and copies the variants to `public/_img/` (see [Before and after](#before-and-after) and [Generated Output](#-generated-output)). Pages without images, SVGs and external URLs are left as they are.
|
|
70
52
|
|
|
71
53
|
## ⚙️ Configuration
|
|
72
54
|
|
|
@@ -109,6 +91,39 @@ prune: true
|
|
|
109
91
|
|
|
110
92
|
An invalid value warns and falls back to its default; an unknown key warns too (typo help). Neither fails the build.
|
|
111
93
|
|
|
94
|
+
## Before and after
|
|
95
|
+
|
|
96
|
+
```md
|
|
97
|
+

|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Without the plugin, this is a bare `<img src="/images/blog/2026/zerpensafari-iphone.jpg" alt="…">`. Phones download the full original. With the plugin:
|
|
101
|
+
|
|
102
|
+
```html
|
|
103
|
+
<picture>
|
|
104
|
+
<source type="image/avif" srcset="/_img/zerpensafari-iphone-fca66340-480.avif 480w, /_img/zerpensafari-iphone-fca66340-800.avif 800w" sizes="100vw">
|
|
105
|
+
<source type="image/webp" srcset="/_img/zerpensafari-iphone-881dc739-480.webp 480w, /_img/zerpensafari-iphone-881dc739-800.webp 800w" sizes="100vw">
|
|
106
|
+
<img src="/_img/zerpensafari-iphone-06076855-800.jpg"
|
|
107
|
+
srcset="/_img/zerpensafari-iphone-06076855-480.jpg 480w, /_img/zerpensafari-iphone-06076855-800.jpg 800w"
|
|
108
|
+
sizes="100vw" width="800" height="1739"
|
|
109
|
+
alt="Startseite der ZerpenSafari"
|
|
110
|
+
loading="eager" fetchpriority="high">
|
|
111
|
+
</picture>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
The `<img>` fallback keeps the source's own format (JPEG stays JPEG, PNG stays PNG; AVIF and TIFF fall back to JPEG) and always points at a processed variant, never the original. Widths are never upscaled. `width`/`height` are the largest variant's size, so the browser reserves the right space before the image loads (no layout shift). Every attribute you set on the original `<img>` (`alt`, `class`, `id`, `data-*`, …) is kept. A missing `alt` stays missing: [`nera check`](https://nera.js.org) reports it (rule `a11y-img-alt`), the plugin does not invent one.
|
|
115
|
+
|
|
116
|
+
On the field-test site (a German/English blog), the 32 content images went from 4.2 MB of originals to 0.45 MB of AVIF a phone downloads, and the Lighthouse mobile LCP of a blog post dropped from 3.5–3.8 s to 2.6 s; with its teaser image rendered through the mixin as well (see below), to 1.8–1.9 s.
|
|
117
|
+
|
|
118
|
+
### What is left untouched
|
|
119
|
+
|
|
120
|
+
- SVG and GIF (animation), external `http(s)://` and `//` URLs, `data:` URIs
|
|
121
|
+
- An `<img>` already inside a `<picture>`, or with its own `srcset`
|
|
122
|
+
- An image with `?skip`, or a raw HTML `<img data-nera-skip>` (the attribute is removed from the output)
|
|
123
|
+
- A file that cannot be found: the build warns with the page and the URL and leaves the `<img>` as it is
|
|
124
|
+
|
|
125
|
+
Raw HTML `<img>` in Markdown is handled like a Markdown image. Images in your **Pug templates** are not processed; use frontmatter images and the mixin for those (see below).
|
|
126
|
+
|
|
112
127
|
## Query parameters
|
|
113
128
|
|
|
114
129
|
Add them to the image URL in Markdown (or in a frontmatter path). They never reach the emitted URLs.
|
|
@@ -140,7 +155,7 @@ An image with a title that is alone in its paragraph becomes:
|
|
|
140
155
|
<figure class="nera-image"><picture>…</picture><figcaption class="nera-image__caption">Usedom, October 2026</figcaption></figure>
|
|
141
156
|
```
|
|
142
157
|
|
|
143
|
-
The `<figure>` replaces the paragraph (a `<figure>` inside `<p>` is invalid HTML), and the `title` attribute is dropped. An image inline with text, inside a link or next to another image keeps its `title` and gets no figure. `figure: false` turns this off. The classes `nera-image` and `nera-image__caption` are part of the public contract: style them freely.
|
|
158
|
+
The `<figure>` replaces the paragraph (a `<figure>` inside `<p>` is invalid HTML), and the `title` attribute is dropped. An image inline with text, inside a link or next to another image keeps its `title` and gets no figure. `figure: false` turns this off. The classes `nera-image` and `nera-image__caption` are part of the public contract: style them freely (see [Styling](#-styling)).
|
|
144
159
|
|
|
145
160
|
## Frontmatter images and the `+picture` mixin
|
|
146
161
|
|
|
@@ -164,13 +179,7 @@ meta.images.image = {
|
|
|
164
179
|
|
|
165
180
|
**`meta.image` itself stays the string you wrote**, so existing templates and plugins keep working. Query parameters work here too (`image: /images/hero.jpg?ratio=16:9&focus=30,60`).
|
|
166
181
|
|
|
167
|
-
Publish the mixin into your views once:
|
|
168
|
-
|
|
169
|
-
```bash
|
|
170
|
-
npx nera-images
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
It lands in `views/vendor/plugin-images/picture.pug` (`theme/views/…` on a themed site). Then, in a layout:
|
|
182
|
+
Publish the mixin into your views once (see [Template Publishing](#️-template-publishing)), then, in a layout:
|
|
174
183
|
|
|
175
184
|
```pug
|
|
176
185
|
include /vendor/plugin-images/picture
|
|
@@ -244,21 +253,95 @@ cache:
|
|
|
244
253
|
|
|
245
254
|
`nera dev` does not watch the cache folder, so it never causes a rebuild loop.
|
|
246
255
|
|
|
247
|
-
##
|
|
256
|
+
## 🛠️ Template Publishing
|
|
257
|
+
|
|
258
|
+
The plugin ships one view, the `+picture` mixin for frontmatter images. Markdown images need no template. To publish it, run from the site's root:
|
|
259
|
+
|
|
260
|
+
```bash
|
|
261
|
+
npx nera-images
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
This copies `picture.pug` to `views/vendor/plugin-images/` (`theme/views/vendor/…` on a site with a `theme/` folder). Publishing skips a `vendor/plugin-images/` folder that already exists, so your edits are never silently overwritten; `npx nera-images --force` overwrites it. A copy published by an older release keeps its old markup: re-publish after an update to pick up changes to the view.
|
|
265
|
+
|
|
266
|
+
## 🎨 Styling
|
|
267
|
+
|
|
268
|
+
The plugin ships no CSS. The markup it writes is part of its public contract, so you can style it from your own stylesheet; renaming a class or changing the structure is a major release.
|
|
269
|
+
|
|
270
|
+
A Markdown image becomes:
|
|
271
|
+
|
|
272
|
+
```html
|
|
273
|
+
<picture>
|
|
274
|
+
<source type="image/avif" srcset="…" sizes="…">
|
|
275
|
+
<source type="image/webp" srcset="…" sizes="…">
|
|
276
|
+
<img src="…" srcset="…" sizes="…" width="…" height="…" alt="…" loading="lazy" decoding="async">
|
|
277
|
+
</picture>
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
- One `<source>` per format in `formats`, in that order. With `formats: []` (or `?formats=none`) there is no `<picture>`, only the `<img>` with its `srcset`.
|
|
281
|
+
- On the `<img>`: `src`, `srcset`, `sizes`, `width`, `height`, then a placeholder's `style` (your own `style` is appended to it), then every other attribute of your original `<img>` (`alt`, `class`, `id`, `data-*`, …), then the loading hints. A lazy image gets `loading="lazy" decoding="async"`, the eager one `loading="eager" fetchpriority="high"`; a `loading`, `decoding` or `fetchpriority` you set yourself wins.
|
|
282
|
+
- A captioned image (see [Captions](#captions)) is wrapped, and the `<figure>` replaces its paragraph:
|
|
283
|
+
|
|
284
|
+
```html
|
|
285
|
+
<figure class="nera-image">
|
|
286
|
+
<picture>…</picture>
|
|
287
|
+
<figcaption class="nera-image__caption">Usedom, October 2026</figcaption>
|
|
288
|
+
</figure>
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
The `+picture` mixin renders the same `<picture>` with its `<source>`s, and an `<img>` with `src`, `srcset`, `sizes`, `width`, `height`, the placeholder's `style`, `alt`, `class`, the loading hints (`loading="lazy" decoding="async"`, or `loading="eager" fetchpriority="high"` with `loading: 'eager'`) and the extra attributes you pass.
|
|
292
|
+
|
|
293
|
+
Because every `<img>` carries `width` and `height`, keep it fluid in your CSS, or a wide image overflows a narrow column:
|
|
294
|
+
|
|
295
|
+
```css
|
|
296
|
+
picture img {
|
|
297
|
+
max-width: 100%;
|
|
298
|
+
height: auto;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
.nera-image__caption {
|
|
302
|
+
font-size: 0.875em;
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## 📊 Generated Output
|
|
307
|
+
|
|
308
|
+
- **Pages:** each processable `<img>` in a page's rendered content is replaced by the markup in [Styling](#-styling). A page without one keeps its exact HTML.
|
|
309
|
+
- **`meta.images.<key>`:** one entry per key in `frontmatter_keys` that holds a processable image (see [Frontmatter images](#frontmatter-images-and-the-picture-mixin)). `meta.image` and the other keys stay as you wrote them. The plugin adds nothing to `app`.
|
|
310
|
+
- **`public/_img/`** (`output_path`): the variants, named `<name>-<hash>-<width>.<ext>`, for example `pier-fca66340-800.avif`. The hash covers the source and the settings, so a URL stays the same as long as the image and its settings do.
|
|
311
|
+
- **`.nera-cache/images/`** (`cache_dir`) in your site: `files/` with the variants and `manifest.json`. Nera copies `files/` into `public/` through the `getAssets` hook; keep the folder out of git.
|
|
312
|
+
|
|
313
|
+
## ⚠️ Warnings
|
|
314
|
+
|
|
315
|
+
A bad config value keeps its default, an image the plugin cannot use is left as it is, and the build goes on; each case prints one line. A build that handled images ends with a summary:
|
|
316
|
+
|
|
317
|
+
```text
|
|
318
|
+
⚠️ plugin-images: config/images.yaml: invalid placeholder "smudge", using "none"
|
|
319
|
+
⚠️ plugin-images: config/images.yaml: ignoring unknown key "width"
|
|
320
|
+
⚠️ plugin-images: /pier.html: image not found, left as is: /images/photos/missing.jpg
|
|
321
|
+
⚠️ plugin-images: /pier.html: unknown parameter "width" ignored: /images/photos/pier.jpg?width=800
|
|
322
|
+
ℹ️ plugin-images: 4 image(s), 12 variant(s) encoded
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## 🧩 Compatibility
|
|
248
326
|
|
|
249
327
|
- **`@nera-static/core` 4.13.0 or later** (the `getAssets` hook, which copies the variants into `public/`). On an older core the build stops with a message naming the required version, rather than publishing pages that link to missing files. A site without any processable image still builds.
|
|
250
|
-
- Node.js
|
|
328
|
+
- Node.js 22.12.0 or later, like `@nera-static/core`.
|
|
251
329
|
- sharp ships prebuilt binaries for macOS, Linux and Windows on x64 and arm64.
|
|
252
330
|
|
|
253
331
|
## 🧪 Development
|
|
254
332
|
|
|
255
333
|
```bash
|
|
334
|
+
npm install
|
|
256
335
|
npx vitest run
|
|
257
336
|
npm run lint
|
|
258
337
|
```
|
|
259
338
|
|
|
260
|
-
The fixture images are generated by sharp at test time (`test/fixtures/make-images.js`), so no binaries are committed.
|
|
339
|
+
`npm run test:watch` runs Vitest in watch mode. `npm run lint` runs [`nera-stan`](https://www.npmjs.com/package/@nera-static/stan), the Nera style check. The integration tests build throwaway sites with the real `@nera-static/core` in a temp directory. The fixture images are generated by sharp at test time (`test/fixtures/make-images.js`), so no binaries are committed.
|
|
340
|
+
|
|
341
|
+
## 🤝 Contributing
|
|
342
|
+
|
|
343
|
+
See the [Nera contributing guide](https://github.com/seebaermichi/nera/blob/main/CONTRIBUTING.md) and the [style guide](https://nera.js.org/docs/style-guide/).
|
|
261
344
|
|
|
262
345
|
## 📦 License
|
|
263
346
|
|
|
264
|
-
MIT
|
|
347
|
+
MIT, see [LICENSE](LICENSE).
|
package/bin/publish-template.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import path from 'path'
|
|
4
|
-
import { fileURLToPath } from 'url'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { fileURLToPath } from 'node:url'
|
|
5
5
|
import { publishTemplates } from '@nera-static/plugin-utils'
|
|
6
6
|
|
|
7
7
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# @nera-static/plugin-images — every key is optional.
|
|
2
|
+
# Copy this file to your site's config/ folder and edit it there.
|
|
3
|
+
|
|
4
|
+
# Which modern formats get a <source>, in order of preference (avif, webp).
|
|
5
|
+
# [] writes a plain <img> with srcset and no <source>.
|
|
6
|
+
formats: [avif, webp]
|
|
7
|
+
|
|
8
|
+
# Candidate widths for srcset, in pixels. An image is never upscaled.
|
|
9
|
+
widths: [480, 800, 1200, 1600]
|
|
10
|
+
|
|
11
|
+
# The default sizes attribute.
|
|
12
|
+
sizes: 100vw
|
|
13
|
+
|
|
14
|
+
# Encoder quality per format, 1 to 100.
|
|
15
|
+
quality:
|
|
16
|
+
avif: 50
|
|
17
|
+
webp: 75
|
|
18
|
+
jpeg: 78
|
|
19
|
+
png: 80
|
|
20
|
+
|
|
21
|
+
# The first image on each page loads eagerly with fetchpriority="high".
|
|
22
|
+
eager_first: true
|
|
23
|
+
|
|
24
|
+
# "title" turns  into a <figure>; false turns that off.
|
|
25
|
+
figure: title
|
|
26
|
+
|
|
27
|
+
# Frontmatter keys holding an image path, written to meta.images.<key>.
|
|
28
|
+
frontmatter_keys: [image]
|
|
29
|
+
|
|
30
|
+
# The og:image preset for frontmatter images (fit: cover, contain or inside);
|
|
31
|
+
# false turns it off.
|
|
32
|
+
og:
|
|
33
|
+
width: 1200
|
|
34
|
+
height: 630
|
|
35
|
+
fit: cover
|
|
36
|
+
|
|
37
|
+
# Placeholder while an image loads: none, color or blur.
|
|
38
|
+
placeholder: none
|
|
39
|
+
|
|
40
|
+
# The public URL path the generated files are served from.
|
|
41
|
+
output_path: /_img
|
|
42
|
+
|
|
43
|
+
# The cache folder, relative to the site root, and whether variants no page
|
|
44
|
+
# uses any more are removed from it.
|
|
45
|
+
cache_dir: .nera-cache/images
|
|
46
|
+
prune: true
|
|
47
|
+
|
|
48
|
+
# Parallel encodes; the default is the number of CPUs minus one.
|
|
49
|
+
# concurrency: 4
|
package/index.js
CHANGED
|
@@ -6,211 +6,22 @@
|
|
|
6
6
|
// `meta.images.<key>`, for the `+picture` mixin, both with an optional
|
|
7
7
|
// placeholder. `getAssets` (core >= 4.13.0) then prunes the cache and
|
|
8
8
|
// hands its files to core, which copies them to `public/<output_path>`.
|
|
9
|
-
//
|
|
10
|
-
import fs from 'fs'
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
13
|
-
import { loadConfig } from './src/config.js'
|
|
14
|
-
import { frontmatterImages } from './src/frontmatter.js'
|
|
15
|
-
import { createGenerator } from './src/generate.js'
|
|
16
|
-
import { assertCoreVersion, coreVersion } from './src/guard.js'
|
|
17
|
-
import { parseParams, sizesFor, stripParams } from './src/params.js'
|
|
18
|
-
import { createQueue } from './src/queue.js'
|
|
19
|
-
import { resolveSource, sourceRoots, splitUrl } from './src/resolve-source.js'
|
|
20
|
-
import { pictureData, publicUrl, rewriteHtml } from './src/rewrite-html.js'
|
|
21
|
-
import { warn } from './src/warn.js'
|
|
9
|
+
// `getMetaData` is async because sharp encodes asynchronously; core awaits it.
|
|
10
|
+
import fs from 'node:fs'
|
|
11
|
+
import { log } from './src/log.js'
|
|
12
|
+
import { processPages } from './src/process-pages.js'
|
|
22
13
|
|
|
23
14
|
// What `getMetaData` leaves for `getAssets` in the same build. Core calls the
|
|
24
15
|
// two hooks in that order, once per build, so one slot is enough.
|
|
25
16
|
let current = null
|
|
26
17
|
|
|
27
|
-
|
|
28
|
-
* Throws (once per build, then again for every later image) when core is too
|
|
29
|
-
* old to call `getAssets` (D12). Checked on the first image that will be
|
|
30
|
-
* generated, before anything is encoded, so a site without images still
|
|
31
|
-
* builds on an older core (decided 2026-10-09).
|
|
32
|
-
*/
|
|
33
|
-
function coreCheck(cwd) {
|
|
34
|
-
let result
|
|
35
|
-
return () => {
|
|
36
|
-
if (result === undefined) {
|
|
37
|
-
const version = coreVersion(cwd)
|
|
38
|
-
if (version === null) {
|
|
39
|
-
warn('could not determine the @nera-static/core version; it must be >= 4.13.0')
|
|
40
|
-
}
|
|
41
|
-
try {
|
|
42
|
-
assertCoreVersion(version)
|
|
43
|
-
result = null
|
|
44
|
-
} catch (err) {
|
|
45
|
-
result = err
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
if (result) throw result
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export async function getMetaData({ app, pagesData }) {
|
|
53
|
-
const cwd = process.cwd()
|
|
54
|
-
const config = loadConfig({ cwd, app })
|
|
55
|
-
const roots = sourceRoots({ app, cwd })
|
|
56
|
-
const cache = openCache(path.resolve(cwd, config.cache_dir))
|
|
57
|
-
const generator = createGenerator({
|
|
58
|
-
cache,
|
|
59
|
-
queue: createQueue({ concurrency: config.concurrency }),
|
|
60
|
-
})
|
|
61
|
-
const ensureCore = coreCheck(cwd)
|
|
62
|
-
const options = {
|
|
63
|
-
widths: config.widths,
|
|
64
|
-
formats: config.formats,
|
|
65
|
-
quality: config.quality,
|
|
66
|
-
}
|
|
67
|
-
// D10. The og image (D9) gets none: it is a file for crawlers, not markup.
|
|
68
|
-
const placeholder = { placeholder: config.placeholder }
|
|
18
|
+
export async function getMetaData({ app = {}, pagesData = [] } = {}) {
|
|
69
19
|
current = null
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// warning), `{ skip }` (`?skip`, with the recognized parameters stripped)
|
|
73
|
-
// or `{ file, params }`, ready to generate. Shared by both passes so a
|
|
74
|
-
// frontmatter path reads exactly like a Markdown image URL.
|
|
75
|
-
const prepare = (src, href, label) => {
|
|
76
|
-
const resolved = resolveSource(src, {
|
|
77
|
-
pageHref: href,
|
|
78
|
-
basePath: app.basePath || '',
|
|
79
|
-
roots,
|
|
80
|
-
})
|
|
81
|
-
// Not a local URL: neither the file nor the query is ours.
|
|
82
|
-
if (['empty', 'external', 'data'].includes(resolved.skip)) return null
|
|
83
|
-
|
|
84
|
-
const params = parseParams(splitUrl(src.trim()).search)
|
|
85
|
-
if (params.skip) return { skip: stripParams(src) }
|
|
86
|
-
|
|
87
|
-
if (resolved.skip) {
|
|
88
|
-
if (resolved.skip === 'missing' || resolved.skip === 'outside') {
|
|
89
|
-
warn(`${href}: ${label}not found, left as is: ${src}`)
|
|
90
|
-
}
|
|
91
|
-
return null
|
|
92
|
-
}
|
|
93
|
-
for (const message of params.warnings) {
|
|
94
|
-
warn(`${href}: ${message}: ${src}`)
|
|
95
|
-
}
|
|
96
|
-
ensureCore()
|
|
97
|
-
return { file: resolved.file, params }
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
let images = 0
|
|
101
|
-
const result = []
|
|
102
|
-
// Pages one after another, a page's images concurrently: the queue bounds
|
|
103
|
-
// the encoders, and only one page's source bytes are held at a time.
|
|
104
|
-
for (const page of pagesData) {
|
|
105
|
-
const href = page.meta?.href || '/index.html'
|
|
106
|
-
|
|
107
|
-
const handle = async (src) => {
|
|
108
|
-
const prepared = prepare(src, href, 'image ')
|
|
109
|
-
if (!prepared || prepared.skip) return prepared && { src: prepared.skip }
|
|
110
|
-
try {
|
|
111
|
-
const image = await generator.generate(prepared.file, {
|
|
112
|
-
...options,
|
|
113
|
-
...prepared.params.options,
|
|
114
|
-
}, placeholder)
|
|
115
|
-
return {
|
|
116
|
-
image,
|
|
117
|
-
sizes: sizesFor(prepared.params, config.sizes),
|
|
118
|
-
loading: prepared.params.loading,
|
|
119
|
-
}
|
|
120
|
-
} catch (err) {
|
|
121
|
-
warn(`${href}: could not process ${src}, left as is (${err.message})`)
|
|
122
|
-
return null
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// D9: `meta.images.<key>` from the frontmatter keys. The `<img>`
|
|
127
|
-
// attributes a template sets itself (`loading`, `alt`) are the mixin's
|
|
128
|
-
// business, so `?loading` has nothing to act on here.
|
|
129
|
-
const prepareFrontmatter = async (key, src) => {
|
|
130
|
-
const prepared = prepare(src, href, `frontmatter ${key} `)
|
|
131
|
-
if (!prepared || prepared.skip) return null
|
|
132
|
-
const { params } = prepared
|
|
133
|
-
if (params.loading) {
|
|
134
|
-
warn(`${href}: loading has no effect on a frontmatter image, ignored: ${src}`)
|
|
135
|
-
}
|
|
136
|
-
try {
|
|
137
|
-
const image = await generator.generate(prepared.file, {
|
|
138
|
-
...options,
|
|
139
|
-
...params.options,
|
|
140
|
-
}, placeholder)
|
|
141
|
-
const entry = pictureData(image, {
|
|
142
|
-
outputPath: config.output_path,
|
|
143
|
-
sizes: sizesFor(params, config.sizes),
|
|
144
|
-
})
|
|
145
|
-
if (config.og) {
|
|
146
|
-
entry.og = await ogImage(generator, prepared.file, config, params)
|
|
147
|
-
}
|
|
148
|
-
return entry
|
|
149
|
-
} catch (err) {
|
|
150
|
-
warn(`${href}: could not process frontmatter ${key} ${src} (${err.message})`)
|
|
151
|
-
return null
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const [rewritten, frontmatter] = await Promise.all([
|
|
156
|
-
rewriteHtml(page.content, {
|
|
157
|
-
handle,
|
|
158
|
-
outputPath: config.output_path,
|
|
159
|
-
sizes: config.sizes,
|
|
160
|
-
eagerFirst: config.eager_first,
|
|
161
|
-
figure: config.figure,
|
|
162
|
-
}),
|
|
163
|
-
frontmatterImages(page.meta, {
|
|
164
|
-
keys: config.frontmatter_keys,
|
|
165
|
-
prepare: prepareFrontmatter,
|
|
166
|
-
}),
|
|
167
|
-
])
|
|
168
|
-
images += rewritten.count + frontmatter.count
|
|
169
|
-
|
|
170
|
-
const changed = rewritten.html !== page.content || frontmatter.meta !== page.meta
|
|
171
|
-
result.push(
|
|
172
|
-
changed ? { ...page, content: rewritten.html, meta: frontmatter.meta } : page
|
|
173
|
-
)
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
current = { config, cache, generator }
|
|
177
|
-
if (images > 0) {
|
|
178
|
-
const placeholders = config.placeholder === 'none'
|
|
179
|
-
? ''
|
|
180
|
-
: `, ${generator.placeholders} placeholder(s) computed`
|
|
181
|
-
console.log(
|
|
182
|
-
`🖼️ plugin-images: ${images} image(s), ${generator.encoded} variant(s) encoded${placeholders}`
|
|
183
|
-
)
|
|
184
|
-
}
|
|
20
|
+
const { pagesData: result, build } = await processPages({ app, pagesData, cwd: process.cwd() })
|
|
21
|
+
current = build
|
|
185
22
|
return result
|
|
186
23
|
}
|
|
187
24
|
|
|
188
|
-
/**
|
|
189
|
-
* The og preset (D9): one JPEG at `og.width` × `og.height` with `og.fit`,
|
|
190
|
-
* never upscaled. Only the frontmatter path's `focus` and `q` carry over; its
|
|
191
|
-
* own geometry (`w`, `ratio`, …) is for the page, not for the social card.
|
|
192
|
-
*/
|
|
193
|
-
async function ogImage(generator, file, config, params) {
|
|
194
|
-
const { width, height, fit } = config.og
|
|
195
|
-
const { focus, q } = params.options
|
|
196
|
-
const image = await generator.generate(file, {
|
|
197
|
-
widths: [width],
|
|
198
|
-
formats: [],
|
|
199
|
-
quality: config.quality,
|
|
200
|
-
ratio: width / height,
|
|
201
|
-
fit,
|
|
202
|
-
focus,
|
|
203
|
-
q,
|
|
204
|
-
fallback: 'jpeg',
|
|
205
|
-
})
|
|
206
|
-
const [variant] = image.variants
|
|
207
|
-
return {
|
|
208
|
-
src: publicUrl(config.output_path, variant.file),
|
|
209
|
-
width: variant.width,
|
|
210
|
-
height: variant.height,
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
25
|
export async function getAssets() {
|
|
215
26
|
if (!current) return []
|
|
216
27
|
const { config, cache, generator } = current
|
|
@@ -225,7 +36,7 @@ export async function getAssets() {
|
|
|
225
36
|
} catch (err) {
|
|
226
37
|
// The variants on disk are complete (atomic writes); a stale
|
|
227
38
|
// manifest only costs a re-check next build. Not worth failing.
|
|
228
|
-
warn(`could not update the image cache (${err.message})`)
|
|
39
|
+
log.warn(`could not update the image cache (${err.message})`)
|
|
229
40
|
}
|
|
230
41
|
}
|
|
231
42
|
|
package/package.json
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nera-static/plugin-images",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A plugin for Nera static site generator to generate responsive, modern-format images (AVIF/WebP, srcset, width/height, lazy loading) at build time.",
|
|
5
5
|
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"publish-template": "node ./bin/publish-template.js",
|
|
8
|
-
"lint": "eslint .",
|
|
9
|
-
"test": "vitest",
|
|
10
|
-
"prepare": "husky"
|
|
11
|
-
},
|
|
12
6
|
"type": "module",
|
|
13
7
|
"exports": {
|
|
14
8
|
".": "./index.js"
|
|
@@ -16,8 +10,16 @@
|
|
|
16
10
|
"bin": {
|
|
17
11
|
"nera-images": "bin/publish-template.js"
|
|
18
12
|
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"publish-template": "node ./bin/publish-template.js",
|
|
15
|
+
"lint": "nera-stan",
|
|
16
|
+
"test": "vitest run",
|
|
17
|
+
"test:watch": "vitest",
|
|
18
|
+
"prepare": "husky"
|
|
19
|
+
},
|
|
19
20
|
"files": [
|
|
20
21
|
"bin",
|
|
22
|
+
"config",
|
|
21
23
|
"index.js",
|
|
22
24
|
"src",
|
|
23
25
|
"views",
|
|
@@ -47,23 +49,24 @@
|
|
|
47
49
|
"publishConfig": {
|
|
48
50
|
"access": "public"
|
|
49
51
|
},
|
|
52
|
+
"sideEffects": false,
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=22.12.0"
|
|
55
|
+
},
|
|
50
56
|
"peerDependencies": {
|
|
51
57
|
"@nera-static/core": ">=4.13.0"
|
|
52
58
|
},
|
|
53
59
|
"dependencies": {
|
|
54
|
-
"@nera-static/plugin-utils": "^1.
|
|
60
|
+
"@nera-static/plugin-utils": "^1.10.0",
|
|
55
61
|
"cheerio": "^1.2.0",
|
|
56
62
|
"sharp": "^0.35.5"
|
|
57
63
|
},
|
|
58
|
-
"engines": {
|
|
59
|
-
"node": ">=20.18.1"
|
|
60
|
-
},
|
|
61
64
|
"devDependencies": {
|
|
62
|
-
"@nera-static/core": "^4.
|
|
63
|
-
"
|
|
65
|
+
"@nera-static/core": "^4.20.0",
|
|
66
|
+
"@nera-static/stan": "^1.2.0",
|
|
67
|
+
"@nera-static/test-utils": "^1.1.0",
|
|
64
68
|
"husky": "^9.1.7",
|
|
65
69
|
"pug": "^3.0.4",
|
|
66
|
-
"vitest": "^5.
|
|
67
|
-
}
|
|
68
|
-
"sideEffects": false
|
|
70
|
+
"vitest": "^5.1.2"
|
|
71
|
+
}
|
|
69
72
|
}
|